package eu.fishcode.android.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.io.IOException;
import java.util.Enumeration;
import java.util.logging.Level;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.border.TitledBorder;

import eu.fishcode.android.ui.component.FileBrowse;
import eu.fishcode.android.ui.component.Dialog;
import eu.fishcode.android.utils.Configuration;
import eu.fishcode.android.utils.Utils;

public class SettingsDialog extends Dialog implements ActionListener {

	public SettingsDialog() {
		super();
		this.setTitle(Utils.getLabel("settings.title"));
		this.setDescription(Utils.getLabel("settings.description"));
		this.setIcon(Utils.getIcon("settings.png"));

		final JPanel panelCenter = new JPanel();
		panelCenter.setLayout(new BoxLayout(panelCenter, BoxLayout.PAGE_AXIS));

		TitledBorder title = BorderFactory.createTitledBorder(Utils
				.getLabel("settings.ui"));
		final JPanel panelUI = new JPanel();
		panelUI.setBorder(title);
		panelUI.setLayout(new BoxLayout(panelUI, BoxLayout.PAGE_AXIS));

		Enumeration keys = Configuration.getkeys();
		while (keys.hasMoreElements()) {
			String str = (String) keys.nextElement();
			if (str.contains("settings.ui."))
				if (str.contains("locale")) {
					panelUI.add(getLocaleSettings(str));
				} else if (str.contains("theme")) {
					panelUI.add(getThemeSettings(str));
				}
		}

		title = BorderFactory.createTitledBorder(Utils
				.getLabel("settings.path"));
		final JPanel panelPath = new JPanel();
		panelPath.setBorder(title);
		panelPath.setLayout(new BoxLayout(panelPath, BoxLayout.PAGE_AXIS));

		keys = Configuration.getkeys();
		while (keys.hasMoreElements()) {
			String str = (String) keys.nextElement();
			if (str.contains("settings.path."))
				panelPath.add(getFileSettings(str));
		}
		
		panelCenter.add(panelUI);
		panelCenter.add(panelPath);
		panelCenter.add(Box.createVerticalGlue());
		this.add(panelCenter, BorderLayout.CENTER);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().contains("settings.ui.")) {
			final JComboBox cb = (JComboBox) e.getSource();
			final JLabel lbl = (JLabel) cb.getSelectedItem();
			Configuration.setString(e.getActionCommand(), lbl.getText());
		} else if (e.getActionCommand().contains("settings.path.")) {
			final FileBrowse fb = (FileBrowse) e.getSource();
			Configuration.setString(e.getActionCommand(), fb.getValue());
		}
	}

	private JPanel getLocaleSettings(String key) {
		String value = Configuration.getString(key);
		final String[] languages = {"Default","Italiano","English","Français","Deutsch","Espańol"};

		JPanel locale = new JPanel();
		locale.setLayout(new BoxLayout(locale, BoxLayout.LINE_AXIS));
		locale.setBorder(BorderFactory.createEmptyBorder(2, 5, 5, 5));
		locale.setPreferredSize(new Dimension(500, 30));
		locale.setMaximumSize(new Dimension(1000,30));
		locale.setMinimumSize(new Dimension(200,30));

		JLabel label = new JLabel(Utils.getLabel(key));
		label.setPreferredSize(new Dimension(200, 26));

		JComboBox combo = new JComboBox();
		combo.setPreferredSize(new Dimension(200, 26));
		combo.setRenderer(new Utils.LabelListRenderer());

		for (String lang : languages) {
			final JLabel themeItem = new JLabel(lang, Utils.getIcon(lang.substring(0,2).toLowerCase() +".png"),SwingConstants.LEFT);
			if (lang.equals("Default")) {
				themeItem.setIcon(null);
			}
			combo.addItem(themeItem);
					if (value.equals(lang)) {
						combo.setSelectedItem(themeItem);
					}
		}

		combo.addActionListener(this);
		combo.setActionCommand(key);
		
		locale.add(label);
		locale.add(Box.createHorizontalGlue());
		locale.add(combo);
		return locale;
	}

	private JPanel getThemeSettings(String key) {
		String value = Configuration.getString(key);
		
		JPanel theme = new JPanel();
		theme.setLayout(new BoxLayout(theme, BoxLayout.LINE_AXIS));
		theme.setBorder(BorderFactory.createEmptyBorder(2, 5, 5, 5));
		theme.setPreferredSize(new Dimension(500, 30));
		theme.setMaximumSize(new Dimension(1000,30));
		theme.setMinimumSize(new Dimension(200,30));
		
		JLabel label = new JLabel(Utils.getLabel(key));
		label.setPreferredSize(new Dimension(200, 26));
		
		JComboBox combo = new JComboBox();
		combo.setPreferredSize(new Dimension(200, 26));
		combo.setRenderer(new Utils.LabelListRenderer());
		
		for (final LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
			final JLabel themeItem = new JLabel(info.getName());
			combo.addItem(themeItem);
			if (value.equals(info.getName())) {
				combo.setSelectedItem(themeItem);
			}
		}

		combo.addActionListener(this);
		combo.setActionCommand(key);
		
		theme.add(label);
		theme.add(Box.createHorizontalGlue());
		theme.add(combo);
		return theme;
	}
	
	private JPanel getFileSettings(String key) {
		String value = Configuration.getString(key);
		
		JPanel file = new JPanel();
		file.setLayout(new BoxLayout(file, BoxLayout.LINE_AXIS));
		file.setBorder(BorderFactory.createEmptyBorder(2, 5, 5, 5));
		file.setPreferredSize(new Dimension(500, 30));
		file.setMaximumSize(new Dimension(1000,30));
		file.setMinimumSize(new Dimension(200,30));
		
		JLabel label = new JLabel(Utils.getLabel(key));
		label.setPreferredSize(new Dimension(200, 26));
		
		FileBrowse browse = new FileBrowse();
		browse.setValue(value);
		
		browse.addActionListener(this);
		browse.setActionCommand(key);
		
		file.add(label);
		file.add(Box.createHorizontalGlue());
		file.add(browse);
		return file;
	}
}
